layui.use(['table', 'util', 'layer'], function(){
    var table = layui.table,
        util = layui.util,
        layer = layui.layer;

    var questionTable = table.render({
        elem: '#testTable'
        ,url:'/question/selectByPage'
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,title: '题库数据表'
        ,method: 'get'
        ,limits: [10,15,20,30]
        ,limit: 10
        ,id:'testTable'
        ,height: 523
        ,cellMinWidth: 80
        ,cols: [[
            {field:'id', title:'序号', align:'center',type: 'numbers', width: 100, unresize: true, sort: true}
            ,{field:'qtitle', title:'题目',align:'center'}
            ,{field:'qanswer1', title:'选项A',align:'center'}
            ,{field:'qanswer2', title:'选项B',align:'center'}
            ,{field:'qanswer3', title:'选项C',align:'center'}
            ,{field:'qanswer4', title:'选项D',align:'center'}
            ,{field:'qresult', title:'正确答案',align:'center'}
            ,{field:'qtype', title:'题目类型',align:'center', templet: function (res) {
                    return res.qtype == "1" ? "单选题" : (res.qtype == "2" ? "多选题" : "简答题");
                }}
            ,{field:'qanalyze', title:'题目解析', templet:function (res) {
                    return res.qanalyze == "" ? "<span style='color: #e1e1e1'>无解析</span>" : res.qanalyze
                }}
            ,{field:'qcreateTime', title:'创建时间',align:'center', width:160,templet: function (res) {
                    return util.toDateString(res.qcreatetime, "yyyy-MM-dd");
                }}
            ,{field:'qscore', title:'分值',align:'center'}
            ,{field:'qstatus', title:'题目状态',align:'center', templet: function (res) {
                    return res.qstatus == "0" ? "<span style='color: red;'>禁用</span>" : "<span>启用</span>";
                }}
            ,{field:'uname', title:'创建人',align:'center'}
            ,{fixed: 'right', title:'操作',align:'center',toolbar: '#barDemo'}
        ]]
        ,page: true
        ,response: {
            "code": 0,
            "msg": "",
            "count": 1000,
            "data": []
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.data.code, //解析接口状态
                "msg": res.data.msg, //解析提示文本
                "count": res.data.count, //解析数据长度
                "data": res.data.data //解析数据列表
            };
        }

    });

    //头工具栏事件
    table.on('toolbar(test)', function(obj){
        switch(obj.event){
            case 'addQuestion':
                window.location.href="/addQuestion";
                break;
            case 'reloadPage':
                var pageIndex = $(".layui-laypage-em").next().html();
                questionTable.reload({
                    method:'get'
                    ,page: {
                        curr: pageIndex //重新从第 当前页 开始
                    }
                });
                break;
        }
    });

    $('.do_search').on('click', function () {
        // 搜索条件
        var qTitle=$(".qTitle").val();
        var uId=$(".uId").val();
        var qType=$(".qType").val();
        var qStatus=$(".qStatus").val();
        questionTable.reload({
            method:'get'
            ,where: { //设定异步数据接口的额外参数，任意设
                qStatus: qStatus,
                qType: qType,
                uId: uId,
                qTitle: qTitle
            }
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    });

    //监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        if(obj.event === 'ban'){
            //获取table表格当前页码
            var pageIndex = $(".layui-laypage-em").next().html()
                ,status = "0"
                ,msg = "是否禁用该题目？";
            if (data.qstatus === "0"){
                status = "1";
                msg = "是否启用该题目？";
            }
            layer.confirm(msg,function(index){
                $.ajax({
                    url: '/question/updateStatus/' + status + '/' + data.qid,
                    type: 'put',
                    data: '',
                    success: function (res) {
                        if (res.code == 200){
                            layer.close(index);
                            questionTable.reload({
                                method:'get'
                                ,page: {
                                    curr: pageIndex //重新从第 当前页 开始
                                }
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });
            });
        } else if(obj.event === 'edit'){
            layer.open({
                title:data.qtype == "1" ? "单选题" : (data.qtype == "2" ? "多选题" : "简答题"),
                type: 2,
                area: ['675px', '575px'],
                fixed: false, //不固定
                maxmin: true,
                content: '/updateQuestion/' + data.qid
            });
        }
    });

    var _tools = {
        reloadTable : function () {
            var pageIndex = $(".layui-laypage-em").next().html();
            questionTable.reload({
                method:'get'
                ,page: {
                    curr: pageIndex //重新从第 当前页 开始
                }
            });
        }
    };
    window.tools = _tools;
});